package com.mock.webscraper.parser;

import com.mock.Pojo.DataItem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelWriter {

    // 将数据写入 Excel 文件
    public static void writeDataToExcel(String filePath, String sheetName,String[] headers , List<DataItem> dataItems) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet(sheetName);

        // 创建标题行
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据
        for (int i = 0; i < dataItems.size(); i++) {
            Row dataRow = sheet.createRow(i + 1);
            DataItem item = dataItems.get(i);
            dataRow.createCell(0).setCellValue(item.getTitle());
            dataRow.createCell(1).setCellValue(item.getDetails());
        }

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
            workbook.write(fileOut);
            System.out.println("数据成功写入 Excel 文件: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        String filePath = "output.xlsx"; // 指定 Excel 文件路径
        String sheetName = "数据"; // 指定工作表名称

        // 示例数据
        List<DataItem> dataItems ;

    }
}
